<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>

<body>
	<script>
		let foo = {
			name: 'leeper',
			age: 20,
			friends: ["黄晓明", "范冰冰"]
		}
		let bar = foo;
		// console.log(foo === bar);
		foo.age = 19;
		console.log(foo);
		console.log(bar)

		// let bar1 = clone(foo)
		// 浅拷贝
		let abc = { ...foo }

		console.log(abc.friends[0])
		// 最简单的方式,深拷贝
		// let bar = JSON.parse(JSON.stringify(foo))



		function clone(obj) {
			if (typeof obj !== 'object') return;
			let newObj = obj instanceof Array ? [] : {};

			for (let key in obj) {
				if (obj.hasOwnProperty(key)) {
					newObj[key] = typeof obj[key] === 'object' ? clone(obj[key]) : obj[key];
				}
			}
			return newObj;

		}

		var newobj = clone(foo)
		foo.friends[0] = "吴亦凡"
		foo.age = 100;
	</script>
</body>

</html>